﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_CarInsurance_AddNewCarSexAge : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCISXAG";
    public static Int64 _providerCarSexAgeid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2;
            ClearProcess();
            if (Request.Params["CSAID"] != null)
            {
                Int32 _CarSexAgeid;
                if (Int32.TryParse(Request.Params["CSAID"].ToString(), out _CarSexAgeid))
                {
                    ShowCarSexAge(_CarSexAgeid);
                }
            }
        }
    }

    private void ShowCarSexAge(Int32 ProviderCarSexAgeId)
    {
        CarSexAge _CarSexAge = new CarSexAge();
        CarSexAgeDB _CarSexAgedb = new CarSexAgeDB();

        _CarSexAge = _CarSexAgedb.GetProviderCarSexAge(ProviderCarSexAgeId);

        if (_CarSexAge != null)
        {
            _providerCarSexAgeid = Convert.ToInt32(_CarSexAge.ProviderCarSexAgeId.ToString());
            lblProviderCarSexAgeId.Text = _CarSexAge.ProviderCarSexAgeId.ToString();
            ddlGender.SelectedValue = _CarSexAge.Sex;
            txtFromAge.Text = _CarSexAge.FromAge.ToString();
            txtToAge.Text = _CarSexAge.ToAge.ToString();
            txtRate.Text = _CarSexAge.SexAgeRate.ToString();
            chkStatus.Checked = Convert.ToBoolean(_CarSexAge.Status);

            if (_CarSexAge.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _CarSexAge.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }


            usrEffectivityDate.Day = _CarSexAge.DInfo1.Day;
            usrEffectivityDate.Month = _CarSexAge.DInfo1.Month;
            usrEffectivityDate.Year = _CarSexAge.DInfo1.Year;


        }
    }

    public void ClearProcess()
    {
        _providerCarSexAgeid = 0;
        lblProviderCarSexAgeId.Text = "0";
        ddlGender.SelectedValue = "-1";
        txtFromAge.Text = "";
        txtToAge.Text = "";
        txtRate.Text = "";
        chkStatus.Checked = true;
        lblMessage.Text = "";

    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarSexAge _CarSexAge = new CarSexAge();
                CarSexAgeDB _CarSexAgedb = new CarSexAgeDB();

                int _trantype = 1;
                _CarSexAge.ProviderCarSexAgeId = Convert.ToInt64(lblProviderCarSexAgeId.Text);
                if (_CarSexAge.ProviderCarSexAgeId > 0)
                {
                    _trantype = 2;
                }
                _CarSexAge.Sex = ddlGender.SelectedValue;
                _CarSexAge.FromAge = Convert.ToInt32(txtFromAge.Text.Trim());
                _CarSexAge.ToAge = Convert.ToInt32(txtToAge.Text.Trim());
                _CarSexAge.SexAgeRate = float.Parse(txtRate.Text.Trim());
                _CarSexAge.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                _CarSexAge.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                _CarSexAge.Status = Convert.ToBoolean(chkStatus.Checked.ToString());

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _CarSexAge.FInfo1 = float.Parse(txtNewRate.Text);
                    _CarSexAge.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _CarSexAge.FInfo1 = float.Parse(txtRate.Text);
                    _CarSexAge.DInfo1 = CE.Global.General.GetUTCDateTime();

                  
                }
                _CarSexAge.ProviderCarSexAgeId = _CarSexAgedb.Insert(_CarSexAge);

                if (_CarSexAge.ProviderCarSexAgeId > 0)
                {
                    InsertProviderAuthTransaction(_CarSexAge.ProviderCarSexAgeId, "CICarSexAge", _trantype, 100, _authcode, _CarSexAge.ProviderCompanyId, _CarSexAge.RefCEUserId);
                    ProviderUpdateSave(_CarSexAge.ProviderCarSexAgeId);
                    ClearProcess();
                    lblMessage.Text = "Saved successfully!";
                }
                else
                {
                    lblMessage.Text = "Saving failed!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarSexAge";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    protected void ibtBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewProviderCarSexAge.aspx");
    }

    protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {
        rfvAuthCode.Validate();
        if (lblProviderCarSexAgeId.Text != "0" && txtAuthCode.Text.Trim().Length > 0)
        {
            //Int64 _caryearid = CheckCarYear(Convert.ToInt32(ddlFromYear.Text), Convert.ToInt32(ddlToYear.Text), Convert.ToInt32(ddlOccupation.Text));
            Int64 _carsexageid = Convert.ToInt64(lblProviderCarSexAgeId.Text);
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());

            if (CheckAuthCode(_authcode, 100))
            {
                if (_carsexageid != 0)
                {
                    CarSexAgeDB _carsexagedb = new CarSexAgeDB();
                    if (_carsexagedb.Delete(_carsexageid, _providercompanyid))
                    {
                        InsertProviderAuthTransaction(_carsexageid, "CICarSexAge", 3, 100, _authcode, _providercompanyid, _refceuserid);
                        Response.Redirect("ViewProviderCarSexAge.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }


    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }


    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
